﻿using System;
using System.Runtime.InteropServices;
using OpenCvSharp;
using SampleBase;

namespace SamplesCS
{
    class VideoCaptureSampleLianJiangGuanPianChuYao : ISample
    {
        public void Run()
        {
            // Opens MP4 file (ffmpeg is probably needed)
            //var capture = new VideoCapture(FilePath.Movie.Bach);
            var capture = new VideoCapture("rtmp://rtmp.open.ys7.com/openlive/61e96da9f12a4d058f4737d02c42998d");
            #region MyRegion
            //int sleepTime = (int)Math.Round(1000 / capture.Fps);

            //using (var window = new Window("capture"))
            //{
            //    // Frame image buffer
            //    Mat image = new Mat();

            //    // When the movie playback reaches end, Mat.data becomes NULL.
            //    while (true)
            //    {
            //        capture.Read(image); // same as cvQueryFrame
            //        if (image.Empty())
            //            break;

            //        window.ShowImage(image);
            //        Cv2.WaitKey(sleepTime);
            //    }
            //} 
            #endregion

            using (var mog = BackgroundSubtractorMOG.Create())
            using (var windowSrc = new Window("src"))
            using (var windowinrange = new Window("inrange"))
            using (var windowDst = new Window("dst"))
            {
                var frame = new Mat();
                var inrange = new Mat();
                var fg = new Mat();
                while (true)
                {
                    capture.Read(frame);
                    if (frame.Empty())
                        break;

                    Cv2.InRange(frame, new Scalar(100, 100, 0), new Scalar(255, 255, 100), inrange);

                    mog.Apply(inrange, fg, 0.01);

                    windowSrc.Image = frame;
                    windowinrange.Image = inrange;

                    windowDst.Image = fg;
                    Cv2.WaitKey(50);
                }
            }
        }

    }
}
